package Work.com;

import java.util.Scanner;

public class Exam04 {
    public static boolean piPei(String string){
        char[] arr = new char[string.length()];
        int count = 0 ;
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            if (c == '('){
                arr[count ++] = c;
            }else if (c == ')'){
                if (count == 0){
                    return false;
                }
                if (arr[count - 1] == '('){
                    arr[-- count] = ' ';
                }
            }
        }
        return true;
    }
    public static void main (String[]args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字符串：");
        String str = sc.next();
        boolean isValid = piPei(str);
        if (isValid) {
            System.out.println(str + "是有效字符串");
        } else {
            System.out.println(str + "不是有效字符串");
        }
    }
}
